<?php
namespace Purse\Model;
use Think\Model;
class RedPacketsModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_redpackets'; 
	
	
	public function getPacketsList($data){
		$map = array();
		if(!empty($data['packetsid'])){
			$map['packetsid'] = $data['packetsid'];
		}
	
		if(!empty($data['custid'])){
			$map['custid'] = $data['custid'];
		}
	
		if(!empty($data['year'])){
			//$map['createtime'] = array('egt' , $data['year']);
			//$map['createtime'] = array('elt' , $data['year']+1);
			
			$map['createtime'] = array('between',array("{$data['year']}",$data['year']+1));
		}
	
	
		$totalamount = $this->field('custid,sum(amount) as totalamount')->where($map)->group('custid')->select();
		$total = $this->where($map)->count();
		$fields = array('packetsid','custid','amount','openamount','number','opennumber','type','sendtime','code','createtime','updatetime');
	
		if(empty($data['sortField']))$data['sortField'] = "packetsid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
	
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
	
		return array('totalamount'=>$totalamount , 'total'=>$total , 'data'=>$result);
	}
	
	public function getExpireList(){
		//$map['createtime'] = array('egt' , date("Y-m-d H:i:s" , time()-86400*3));
		//$map['createtime'] = array('elt' , date("Y-m-d H:i:s" , time()-86400));
		
		$map['createtime'] = array('between',array(date("Y-m-d H:i:s" , time()-86400*3),date("Y-m-d H:i:s" , time()-(int)C('RED_PACKETS_EXPIRE'))));
		$map['isreturn'] = 0;
		
		$fields = array('packetsid','custid','amount','openamount','number','opennumber','type','sendtime','code','createtime','updatetime');
		$result = $this->field($fields)->where($map)->select();
		return $result;
	}
	
	public function opennumberInc($packetsid , $amount){
		$sql = "update qyx_redpackets set opennumber=opennumber+1,openamount=openamount+{$amount} where packetsid={$packetsid}";
		return $this->execute($sql);
	}
	
	public function addPackets($data){
		$result = $this->add($data);
		return $result;
	}
	
	
	public function updatePackets($data){
		$map['packetsid'] = $data['packetsid'];
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	
	public function getNewPacketsById($packetsid){
		$map['packetsid'] = $packetsid;
		$map['chatid']  = 0;
		$map['tcustid']  = 0;
		$result = $this->field('packetsid,custid,blessing,chatid,tcustid,amount,openamount,number,opennumber,type,sendtime,code,createtime,updatetime')->where($map)->find();
		return $result;
	}
	
	
	public function getPacketsById($packetsid){
		$map['packetsid'] = $packetsid;
		$result = $this->field('packetsid,custid,blessing,chatid,tcustid,amount,openamount,number,opennumber,type,sendtime,code,createtime,updatetime')->where($map)->find();
		return $result;
	}
}


